﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace TowerDefense_MLP
{
    public partial class main : Form
    {
        GameController gameController;
        Tower selectedTower;
        int towerId;
        bool gameBegin;

        public main()
        {
            InitializeComponent();
            loadInitialPage();

            this.selectedTower = null;
        }

        private void loadInitialPage()
        {
            Image capaImg = Image.FromFile(@"images\capa.jpg");
            panelGame.BackgroundImage = capaImg;
            panelGame.Size = new Size(capaImg.Width, capaImg.Height);

            Image btnNotPressedImg = Image.FromFile(@"images\playNotPressed.jpg");
            btn_play.BackgroundImage = btnNotPressedImg;

            gameBegin = false;

            panelMenu.Visible = false;

            btn_newGame.Visible = false;
            btn_firstMap.Visible = false;
            btn_Start.Visible = false;
            btn_waterTower.Visible = false;
            btn_canonTower.Visible = false;
            btn_fireTower.Visible = false;
            lbl_life.Visible = false;
            lbl_attack.Visible = false;
            lbl_fireRate.Visible = false;
            btn_upgrade.Visible = false;
            btn_sell.Visible = false;
            lbl_money.Visible = false;
        }

        private void timerIterator_Tick(object sender, EventArgs e)
        {
            this.gameController.Iterate();
            
            panelGame.CreateGraphics().DrawImage(this.gameController.MoveEnemiesImage, new Point(0, 0));
            lbl_life.Text = this.gameController.Lifes.ToString();
            lbl_money.Text = "$ " + this.gameController.Money.ToString();

            if (this.gameController.WaveFinished)
            {
                btn_Start.Enabled = true;
                timerIterator.Enabled = false;
            }

            Consts gameState;
            if (this.gameController.GameOver(out gameState))
            {
                if (gameState == Consts.VICTORY)
                    MessageBox.Show("Parabéns! você venceu!", "Fim de Jogo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                else if (gameState == Consts.DEFEATED)
                    MessageBox.Show("Você perdeu!", "Fim de Jogo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                timerIterator.Enabled = false;
                this.gameBegin = false;
            }
        }

        #region PlayButton Functions
        private void btn_play_MouseUp(object sender, MouseEventArgs e)
        {
            Image btnPressedImg = Image.FromFile(@"images\playPressed.jpg");
            btn_play.BackgroundImage = btnPressedImg;
        }

        private void btn_play_MouseLeave(object sender, EventArgs e)
        {
            Image btnNotPressedImg = Image.FromFile(@"images\playNotPressed.jpg");
            btn_play.BackgroundImage = btnNotPressedImg;
        }

        private void btn_play_Click(object sender, EventArgs e)
        {
            Image initialImg = Image.FromFile(@"images\initial.jpg");
            panelGame.BackgroundImage = initialImg;

            btn_play.Visible = false;
            btn_play.Enabled = false;

            Image newGameNotPressedImg = Image.FromFile(@"images\newGameNotPressed.jpg");
            btn_newGame.BackgroundImage = newGameNotPressedImg;

            btn_newGame.Visible = true;
            btn_newGame.Enabled = true;

            btn_newGame.Location = new Point(191, 156);
            btn_newGame.Size = new Size(141, 43);

        }
        #endregion

        #region NewGameButton Functions
        private void btn_newGame_MouseMove(object sender, MouseEventArgs e)
        {
            Image newGamePressedImg = Image.FromFile(@"images\newGamePressed.jpg");
            btn_newGame.BackgroundImage = newGamePressedImg;
            btn_newGame.Location = new Point(185, 155);
            btn_newGame.Size = new Size(152, 45);
        }

        private void btn_newGame_MouseLeave(object sender, EventArgs e)
        {
            Image newGameNotPressedImg = Image.FromFile(@"images\newGameNotPressed.jpg");
            btn_newGame.BackgroundImage = newGameNotPressedImg;
            btn_newGame.Location = new Point(191, 156);
            btn_newGame.Size = new Size(141, 43);
        }

        private void btn_newGame_Click(object sender, EventArgs e)
        {
            Image chooseMapImg = Image.FromFile(@"images\chooseMap.jpg");
            panelGame.BackgroundImage = chooseMapImg;

            btn_newGame.Visible = false;
            btn_newGame.Enabled = false;

            btn_firstMap.Visible = true;
            btn_firstMap.Enabled = true;
        }
        #endregion

        #region MapSelection Buttons
        private void initializeGameController(int ID)
        {
            this.gameController = new GameController(ID);
            panelGame.BackgroundImage = gameController.MapImage;
            panelGame.Size = new Size(gameController.MapImage.Width, gameController.MapImage.Height);

            lbl_life.Text = gameController.Lifes.ToString();
            lbl_money.Text = "$ " + gameController.Money.ToString();
            btn_firstMap.Visible = false;
           
            gameBegin = true;
            panelMenu.Visible = true;

            lbl_money.Visible = true;
            lbl_life.Visible = true;
            btn_Start.Visible = true;
            btn_waterTower.Visible = true;
            btn_canonTower.Visible = true;
            btn_fireTower.Visible = true;
        }

        private void btn_firstMap_Click(object sender, EventArgs e)
        {
            initializeGameController(0);
        }
        #endregion

        #region PanelFunctions

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.selectedTower != null)
            {
                Pen p;

                if (this.gameController.PositionIsOcuppied(e.X, e.Y))
                    p = new Pen(Color.Red, 3.0f);
                else
                    p = new Pen(Color.Black, 3.0f);

                panelGame.Enabled = false;
                panelGame.Enabled = true;

                panelGame.CreateGraphics().DrawImageUnscaled(this.selectedTower.TowerImage,
                                                          new Point(e.X - (this.selectedTower.TowerImage.Width / 2), e.Y - (this.selectedTower.TowerImage.Height / 2)));

                panelGame.CreateGraphics().DrawEllipse(p, (e.X - this.selectedTower.Range / 2),
                                                          (e.Y - this.selectedTower.Range / 2),
                                                          this.selectedTower.Range,
                                                          this.selectedTower.Range);

            }
        }

        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            if (this.selectedTower != null)
            {
                if (!this.gameController.PositionIsOcuppied(e.X, e.Y))
                {
                    this.gameController.AddTower(this.selectedTower,
                                                  (e.X - (this.selectedTower.TowerImage.Width / 2)),
                                                  (e.Y - (this.selectedTower.TowerImage.Height / 2)));

                    panelGame.BackgroundImage = this.gameController.MapImage;

                    updateMoney(-selectedTower.Value);

                    this.selectedTower = null;
                    this.panelGame.Enabled = false;
                    this.panelGame.Enabled = true;
                }
            }
            else if (gameBegin)
            {
                towerId = this.gameController.ElementAtPosition(e.X, e.Y);

                if (towerId != (int)Consts.EMPTY && towerId != (int)Consts.OCCUPIED)
                    updateTowerAtributes(gameController.TowersOnMap[towerId]);
                else
                    disableClickTowerLabels();
            }
        }

        private void updateTowerAtributes(Tower upgradeOrSellTower)
        {
            panelGame.Enabled = false;
            panelGame.Enabled = true;

            btn_upgrade.ForeColor = (gameController.Money >= upgradeOrSellTower.Value) ? Color.Lime : Color.Red;
            btn_upgrade.Visible = (upgradeOrSellTower.UpgradesLeft != 0) ? true : false;

            panelGame.CreateGraphics().DrawEllipse( new Pen(Color.Black, 3.0f),
                                                    (upgradeOrSellTower.Position.X - upgradeOrSellTower.Range / 2 + 20),
                                                    (upgradeOrSellTower.Position.Y - upgradeOrSellTower.Range / 2 + 20),
                                                    upgradeOrSellTower.Range,
                                                    upgradeOrSellTower.Range);

            lbl_attack.Text = "Attack: " + upgradeOrSellTower.AtackValue.ToString();
            lbl_fireRate.Text = "Fire Rate: " + upgradeOrSellTower.Firerate.ToString();
            btn_upgrade.Text = "Upgrade: " + upgradeOrSellTower.Value.ToString();
            btn_sell.Text = "Sell: " + (upgradeOrSellTower.Value / 2).ToString();

            lbl_attack.Visible = true;
            lbl_fireRate.Visible = true;
            btn_sell.Visible = true;
        }

        private void disableClickTowerLabels()
        {
            lbl_attack.Visible = false;
            lbl_fireRate.Visible = false;
            btn_upgrade.Visible = false;
            btn_sell.Visible = false;
        }

        #endregion

        #region BUTTONS_MENU
        private void btn_canonTower_Click(object sender, EventArgs e)
        {
            this.selectedTower = new CanonTower(0, 0);

            if (gameController.Money >= this.selectedTower.Value)
                disableClickTowerLabels();
            else
                this.selectedTower = null;
        }

        private void btn_waterTower_Click(object sender, EventArgs e)
        {
            this.selectedTower = new WaterTower(0, 0);

            if (gameController.Money >= this.selectedTower.Value)
                disableClickTowerLabels();
            else
                this.selectedTower = null;
        }

        private void btn_fireTower_Click(object sender, EventArgs e)
        {
            this.selectedTower = new FireTower(0, 0);

            if (gameController.Money >= this.selectedTower.Value)
                disableClickTowerLabels();
            else
                this.selectedTower = null;
        }

        private void updateMoney(int value)
        {
            gameController.Money += value;
            lbl_money.Text = "$ " + gameController.Money.ToString();
        }

        private void btn_upgrade_Click(object sender, EventArgs e)
        {
            int value = this.gameController.TowersOnMap[towerId].Value;

            if (this.gameController.UpgradeTower(this.towerId))
            {
                panelGame.BackgroundImage = this.gameController.MapImage;
                updateMoney(-value);
            }
        }

        private void btn_sell_Click(object sender, EventArgs e)
        {
            updateMoney(gameController.TowersOnMap[towerId].Value / 2);
            this.gameController.SellTower(this.towerId);
            panelGame.BackgroundImage = this.gameController.MapImage;
            disableClickTowerLabels();
        }

        private void btn_Start_Click(object sender, EventArgs e)
        {
            if (gameBegin)
            {
                this.gameController.NextWave();
                lbl_waves.Text = this.gameController.ActualWave.ToString() + " / " + this.gameController.NumWaves.ToString();
                timerIterator.Enabled = true;
                btn_Start.Enabled = false;    
            }

        }
        #endregion
    }
}
